package com.anorak.betsy;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import okhttp3.*;

import java.io.IOException;
import java.util.HashMap;

/**
 * Hello world!
 */
public class App {
    private static final String SEARCH_URL = "http://127.0.0.1:8899/qq/search/";
    private static final String DOWNLOAD_URL = "http://127.0.0.1:8899/download";
    public static void main(String[] args) throws IOException {
        OkHttpClient client = new OkHttpClient().newBuilder().build();
        Request request = new Request.Builder()
                .url(SEARCH_URL + "p:693837784/1/1")
                .method("GET", null)
                .build();
        Response response = client.newCall(request).execute();
        assert response.body() != null;
        JSONObject json = new JSONObject(response.body().string().trim());
        JSONArray array = json.getJSONArray("list");
        for (Object object : array) {
            OkHttpClient clientPost = new OkHttpClient().newBuilder().build();
            MediaType mediaType = MediaType.parse("application/json");
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("music", new JSONObject(object.toString()));
            HashMap map = new HashMap<>();
            map.put("onlyMatchSearchKey", false);
            map.put("ignoreNoAlbumSongs", false);
            map.put("classificationMusicFile", false);
            map.put("disableFilterKey", false);
            HashMap temp = new HashMap<>();
            temp.put("num", 16);
            temp.put("downloadFolder", "/Users/anorak/Downloads/music/");
            temp.put("saveLyric", false);
            map.put("concurrency", temp);
            map.put("platform", "qq");
            jsonObject.put("config", map);
            RequestBody body = RequestBody.create(mediaType, jsonObject.toString());
            Request requestPost = new Request.Builder()
                    .url("http://127.0.0.1:8899/download")
                    .method("POST", body)
                    .build();
            Response responsePost = clientPost.newCall(requestPost).execute();
            System.out.println(responsePost.code());
        }
    }
}
